const dotenv =  require('dotenv');
const {join}=require('path');
dotenv.config({path:join(__dirname,'../','.env')});

const port = (process.env.APP_PORT||3000);
const express = require('express')
const bodyParser = require('body-parser')
const  {log}  = require('../log.js')
const app = express()

const uploadImgRouter = require('./router/upload')
const noticeRouter = require('./router/notice')
const tokenRouter = require('./router/token')
const firmwareRouter = require('./router/firmware')
const cameraLoginRouter = require('./router/camera_login')
const getCameraRouter = require('./router/get_camera')


app.all('*', function (req, res, next) {
 // 开发环境设置，生产环境谨慎使用
 res.header("Access-Control-Allow-Origin", "*");
 res.header("Access-Control-Allow-Headers", "X-Requested-With");
 res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
 if (req.method === 'OPTIONS') {
  res.send(200);
 } else {
  next();
 }
});

app
    .use(bodyParser.json())
    .use(bodyParser.urlencoded({extended: true}))
    .use(express.static('public'))
    .use('/upload', uploadImgRouter)
    .use('/notice', noticeRouter)
    .use('/token', tokenRouter)
    .use('/firmware', firmwareRouter)
    .use('/camera-login', cameraLoginRouter)
    .use('/get-camera', getCameraRouter)
    .listen(port, () => log.info(`server listening on port ${port}!`))
